﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Entities.Portals;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;

    public class ImgReSize
    {
        public int _CompositingQuality = 0;
        public int _ImageQuality = 0x55;
        public int _InterpolationMode = 7;
        public int _PixelOffsetMode = 0;
        public int _SmoothingMode = 2;

        public byte[] BmpToBytes_MemStream(Bitmap bmp, long ImageQuality, int ImageEncodersInt)
        {
            MemoryStream stream = new MemoryStream();
            ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders();
            EncoderParameters encoderParams = new EncoderParameters(1);
            encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, ImageQuality);
            bmp.Save(stream, imageEncoders[ImageEncodersInt], encoderParams);
            byte[] buffer = stream.GetBuffer();
            bmp.Dispose();
            stream.Close();
            return buffer;
        }

        public Image BytesToImg(byte[] bmpBytes)
        {
            MemoryStream stream = new MemoryStream(bmpBytes);
            return Image.FromStream(stream);
        }

        public string ConvertImage(PortalSettings PortalSettings, string FileNameIN, string FileNameOUT, double imgSize)
        {
            string str = string.Empty;
            try
            {
                str = this.DoResizeThumb(FileNameIN, SharedFunctions.ReplaceFileExt(FileNameOUT, ".jpg"), imgSize);
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                str = "";
                ProjectData.ClearProjectError();
            }
            return str;
        }

        public string DoResizeThumb(string FileNamePath, string FileNamePathOut, double intMaxWidth)
        {
            string str = string.Empty;
            try
            {
                using (Bitmap bitmap2 = new Bitmap(FileNamePath))
                {
                    Bitmap bitmap;
                    int width = bitmap2.Width;
                    if ((intMaxWidth > 0.0) & ((intMaxWidth < width) | (intMaxWidth < bitmap2.Height)))
                    {
                        int num3;
                        int num4;
                        double num2 = bitmap2.PhysicalDimension.Width / bitmap2.PhysicalDimension.Height;
                        if (bitmap2.PhysicalDimension.Height < bitmap2.PhysicalDimension.Width)
                        {
                            num4 = (int) Math.Round(intMaxWidth);
                            num3 = (int) Math.Round((double) (intMaxWidth / num2));
                        }
                        else
                        {
                            num4 = (int) Math.Round((double) (intMaxWidth * num2));
                            num3 = (int) Math.Round(intMaxWidth);
                        }
                        bitmap = new Bitmap(num4, num3);
                        Graphics graphics = Graphics.FromImage(bitmap);
                        graphics.InterpolationMode = (InterpolationMode) this._InterpolationMode;
                        graphics.SmoothingMode = (SmoothingMode) this._SmoothingMode;
                        graphics.PixelOffsetMode = (PixelOffsetMode) this._PixelOffsetMode;
                        graphics.CompositingQuality = (CompositingQuality) this._CompositingQuality;
                        graphics.FillRectangle(Brushes.White, 0, 0, num4, num3);
                        graphics.DrawImage(bitmap2, 0, 0, num4, num3);
                        bitmap.SetResolution(72f, 72f);
                    }
                    else
                    {
                        bitmap = bitmap2;
                        bitmap.SetResolution(72f, 72f);
                    }
                    string fileName = Path.GetFileName(FileNamePathOut);
                    string replacement = Strings.Replace(fileName, " ", "_", 1, -1, CompareMethod.Binary);
                    FileNamePathOut = Strings.Replace(FileNamePathOut, fileName, replacement, 1, -1, CompareMethod.Binary);
                    FileSystemUtils.SaveFile(FileNamePathOut, this.BmpToBytes_MemStream(bitmap, (long) this._ImageQuality, 1));
                    bitmap.Dispose();
                    if (bitmap2 != null)
                    {
                        bitmap2.Dispose();
                    }
                }
                str = FileNamePathOut;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                str = "";
                ProjectData.ClearProjectError();
            }
            return str;
        }

        public string ResizeImageFile(PortalSettings PortalSettings, string DestFolderPath, string SrcFilePathName, double imgSize)
        {
            string destFileName = "";
            Collection collection = new Collection();
            DestFolderPath = FileSystemUtils.FormatFolderPath(DestFolderPath);
            if (SharedFunctions.IsImageFile(Path.GetExtension(SrcFilePathName)))
            {
                if (((imgSize > 0.0) | (Path.GetExtension(SrcFilePathName) == ".bmp")) | (Path.GetExtension(SrcFilePathName) == ".tiff"))
                {
                    return this.ConvertImage(PortalSettings, SrcFilePathName, DestFolderPath + Path.GetFileName(SrcFilePathName), imgSize);
                }
                if (DestFolderPath.EndsWith("/"))
                {
                    DestFolderPath = DestFolderPath.TrimEnd(new char[] { '/' }) + @"\";
                }
                destFileName = DestFolderPath + Path.GetFileName(SrcFilePathName);
                try
                {
                    File.Move(SrcFilePathName, destFileName);
                }
                catch (Exception exception1)
                {
                    ProjectData.SetProjectError(exception1);
                    Exception exception = exception1;
                    FileSystemUtils.MoveFile(SrcFilePathName, destFileName, PortalSettings);
                    ProjectData.ClearProjectError();
                }
            }
            return destFileName;
        }
    }
}

